package com.wangyonghao.harmonyocrs.slice;

import com.wangyonghao.harmonyocrs.MainAbility;
import com.wangyonghao.harmonyocrs.ResourceTable;
import com.wangyonghao.harmonyocrs.utils.ToastUtils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.components.TextField;
import ohos.agp.window.service.WindowManager;
import ohos.miscservices.pasteboard.PasteData;
import ohos.miscservices.pasteboard.SystemPasteboard;

public class HistoryDetailAbilitySlice extends AbilitySlice {

    TextField time;
    TextField result;
    TextField way;
    Text copy;
    private Image back;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_history_detail);
        //设置切面全屏显示
        getWindow().addFlags(WindowManager.LayoutConfig.MARK_FULL_SCREEN |
                WindowManager.LayoutConfig.MARK_TRANSLUCENT_STATUS |
                WindowManager.LayoutConfig.MARK_TRANSLUCENT_NAVIGATION);
        initComponent();
        event();
        String time = intent.getStringParam("time");
        if (time != null) {
            this.time.setText(time);
        }
        String result = intent.getStringParam("result");
        if (time != null) {
            this.result.setText(result);
        }
        String way = intent.getStringParam("way");
        if (time != null) {
            if (way.equals("camera")) {
                this.way.setText("相册图片");
            } else {
                this.way.setText("相机拍照");
            }
        }
    }

    private void event() {
        back.setClickedListener(e -> {
            Intent intent = new Intent();
            Operation operation = new Intent.OperationBuilder()
                    .withDeviceId("")
                    .withBundleName("com.wangyonghao.harmonyocrs")
                    .withAbilityName(MainAbility.class.getName())
                    .withAction("historyAbilitySlice")
                    .build();
            intent.setOperation(operation);
            startAbility(intent);
        });
        copy.setClickedListener(e->{
            if (result.getText() == null || "".equals(result.getText())) {
                ToastUtils.showDialog(this, "暂无识别内容，请先识别~");
                return;
            }
            SystemPasteboard pasteboard = SystemPasteboard.getSystemPasteboard(getApplicationContext());
            if (pasteboard == null) {
                ToastUtils.showDialog(this, "获取粘贴板失败，请在设置中授予权限！");
            } else {
                pasteboard.setPasteData(PasteData.creatPlainTextData(result.getText().toString()));
                ToastUtils.showDialog(this, "已复制识别结果到粘贴板~");
            }
        });
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }


    private void initComponent() {
        time = (TextField) findComponentById(ResourceTable.Id_history_detail_time);
        result = (TextField) findComponentById(ResourceTable.Id_history_detail_result);
        way = (TextField) findComponentById(ResourceTable.Id_history_detail_way);
        back = (Image) findComponentById(ResourceTable.Id_history_detail_back);
        copy = (Text) findComponentById(ResourceTable.Id_history_detail_copy_result);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    protected void onAbilityResult(int requestCode, int resultCode, Intent resultData) {

    }


}
